package jp.sourceforge.akjrcp.tools;

import jp.sourceforge.akjrcp.commons.ui.FileDirChoicer;
import jp.sourceforge.akjrcp.commons.ui.AbstractFileDirChoicer.FileStatusChangeEvent;
import jp.sourceforge.akjrcp.commons.ui.AbstractFileDirChoicer.FileStatusChangeListener;
import jp.sourceforge.akjrcp.ui.param.ParamText;
import jp.sourceforge.akjrcp.ui.param.ParamTextArea;
import jp.sourceforge.akjrcp.util.ExecutingControler;
import jp.sourceforge.akjrcp.util.ExecutingControler.ExecutingControlButton;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;


public class TextSiteMapsViewer extends ViewPart {
	public static Log log=LogFactory.getLog(TextSiteMapsViewer.class);
	
	public static final String ID = TextSiteMapsViewer.class.getName();
	
	private ParamTextArea ignore_end_name;
	private ParamTextArea ignore_start_name;
	private ParamTextArea igreno_files;
	
	private ParamText base_url;
	private ParamText add_files;
	
	private FileDirChoicer inputDirChooser;

	private FileDirChoicer outputFileChooser;
	private TextSiteMapExecute textSiteMapExecute;
	
	private ParamTextArea result_text;
	public void createPartControl(Composite parent) {
		textSiteMapExecute = new TextSiteMapExecute();
		
		parent.setLayout(new GridLayout(1,false));
		
		igreno_files=createGridTextAreaGroup(parent,"ignore_list","ignore_list",new GridData(GridData.FILL_BOTH));
		Composite s1=createSplitComposite(parent,2,false,new GridData(GridData.FILL_BOTH));
		ignore_start_name=createGridTextAreaGroup(s1,"ignore_start","ignore_start",new GridData(GridData.FILL_BOTH));
		ignore_end_name=createGridTextAreaGroup(s1,"ignore_end","ignore_end",new GridData(GridData.FILL_BOTH));
		
		Composite s2=createSplitComposite(parent,2,false,new GridData(GridData.FILL_HORIZONTAL));
		base_url=createLabeldText(s2,"baseURL","baseURL",new GridData(GridData.FILL_HORIZONTAL));
		add_files=createLabeldText(s2,"addFiles","addFiles",new GridData(GridData.FILL_HORIZONTAL));
		add_files.setValue("html");//TODO wildcard
		
		FileSelected selectedListener=new FileSelected();
		inputDirChooser = new FileDirChoicer(parent,SWT.BORDER,FileDirChoicer.DIR,"input");
		inputDirChooser.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		inputDirChooser.addFileListChangeListener(selectedListener);
		
		outputFileChooser = new FileDirChoicer(parent,SWT.BORDER,FileDirChoicer.SAVE,"txt_file");
		outputFileChooser.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		outputFileChooser.setFilterExtensions(new String[]{"*.txt"});
		outputFileChooser.addFileListChangeListener(selectedListener);
		
		
		
		textSiteMapExecute.addControl(inputDirChooser);
		textSiteMapExecute.addControl(outputFileChooser);
		
		ExecutingControlButton exec=new ExecutingControlButton(parent,SWT.NULL,textSiteMapExecute,
				"exec","cancel");
		
		result_text=createGridTextAreaGroup(parent,"result","result",new GridData(GridData.FILL_BOTH));
	}
	
	public void setFocus() {
		
	}
	private Composite createSplitComposite(Composite parent,int split,boolean same,Object data){
		Composite composite=new Composite(parent,SWT.NULL);
		composite.setLayout(new GridLayout(split,same));
		if(data!=null){
			composite.setLayoutData(data);
		}
		return composite;
	}
	private ParamTextArea createGridTextAreaGroup(Composite parent,String key,String label,Object data){
		/*Group group=new Group(parent,SWT.NULL);
		group.setText(label);
		group.setLayout(new FillLayout());
		group.setLayoutData(data);
		Text text=new Text(group,SWT.MULTI|SWT.V_SCROLL|SWT.H_SCROLL|SWT.BORDER);
		*/
		
		ParamTextArea text=new ParamTextArea(parent,SWT.NULL,key,label,ParamText.LAYOUT_FILL_BOTH);
		text.setLayoutData(data);
		return text;
	}
	
	private ParamText createLabeldText(Composite parent,String key,String label,Object data){
		ParamText text=new ParamText(parent,SWT.NULL,key,label,ParamText.LAYOUT_FILL_HORIZONTAL);
		if(data!=null){
			text.setLayoutData(data);
		}
		return text;
	}
	
	public class TextSiteMapExecute extends ExecutingControler{
		private String ignore_end_name_value;
		private String ignore_start_name_value;
		private String igreno_files_value;
		
		private String base_url_value;
		private String add_files_value;
		
		private String inputDirChooser_value;

		private String outputFileChooser_value;
		
		private StringBuffer buffer=new StringBuffer();
		public void afterExecute() {
			result_text.setValue(buffer.toString());
		}

		@Override
		public void beforeExecute() {
			ignore_end_name_value=ignore_end_name.getValue();
			ignore_start_name_value=ignore_start_name.getValue();
			igreno_files_value=igreno_files.getValue();
			base_url_value=base_url.getValue();
			add_files_value=add_files.getValue();
			inputDirChooser_value=inputDirChooser.getAbsoluteFilePath();
			outputFileChooser_value=outputFileChooser.getAbsoluteFilePath();
		}

		@Override
		public boolean canExecute() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void cancelExecuting() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void inExecute() {
			String[] ignores_start=ignore_start_name_value.split("\n"); //TODO check rn
			String[] ignores_end=ignore_end_name_value.split("\n");
			String[] ignore_paths=igreno_files_value.split("\n");
			
			
			
		}
		
		
	}

	public class FileSelected implements FileStatusChangeListener{

		public void addList(FileStatusChangeEvent event) {
			
			textSiteMapExecute.updateControlSwitch();
		}

		public void removeList(FileStatusChangeEvent event) {
			textSiteMapExecute.updateControlSwitch();
		}
		
	}

	
		
}